package org.shop.controller.app.live;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Praise;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.PraiseService;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PraiseContrller {
	private static Logger logger = LogManager.getLogger(PraiseContrller.class);
	
	@Autowired
	private PraiseService praiseService;
	
    /**
     * app根据条件获取点赞信息
     * @param req
     * @return
     */
    @RequestMapping("app/getPraise")
    @ResponseBody
    public Map<String, Object> getPraise(HttpServletRequest req){
    	Map<String, Object> map = new HashMap<String, Object>();
    	try {
    		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    		String targetId = req.getParameter("targetId");
    		String praiseType = req.getParameter("praiseType");
    		Map<String, Object> params = new HashMap<String, Object>();
    		params.put("targetId", targetId);
    		params.put("userId", ud.getUserId());
    		params.put("praiseType", praiseType);
    		
    		Praise praise = praiseService.getPraise(params);
    		if(ObjUtil.isNotNullAndEmpty(praise)) {
    			map.put("state", 1);
    			map.put("msg", "已点赞");
    			map.put("praise", praise);
    		}
    		
    	} catch(Exception e){
    		e.printStackTrace();
    		logger.error(e);
    	}
		return map;
    }
    
    /**
     * app帖子和评论点赞/取消点赞
     * @param req
     * @return
     */
    @RequestMapping("app/addPraise")
    @ResponseBody
    public Map<String, Object> addPraise(HttpServletRequest req){
    	Map<String, Object> map = new HashMap<String, Object>();
    	try {
    		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    		String postId = req.getParameter("postId");
    		String targetId = req.getParameter("targetId");
    		String praiseType = req.getParameter("praiseType");
    		Map<String, Object> params = new HashMap<String, Object>();
    		params.put("postId", postId);
    		params.put("targetId", targetId);
    		params.put("userId", ud.getUserId());
    		params.put("praiseType", praiseType);
    		Praise praise = praiseService.getPraise(params);
    		if(ObjUtil.isNotNullAndEmpty(praise)) {
    			praiseService.deleteByPrimaryKey(praise.getPraiseId());
    			map.put("state", 2);
        		map.put("msg", "取消点赞成功");
    		}else {
    			Praise p = new Praise();
    			p.setPostId(Long.parseLong(postId));
    			p.setTargetId(Long.parseLong(targetId));
    			p.setPraiseType(Byte.parseByte(praiseType));
    			p.setUserId(ud.getUserId());
    			praiseService.insertSelective(p);
    			map.put("state", 1);
    			map.put("msg", "点赞成功");
    		}
    		
    	} catch(Exception e){
    		e.printStackTrace();
    		logger.error(e);
    	}
		return map;
    }
    
    /**
     * app根据targetId获取点赞量
     * @param req
     * @return
     */
    @RequestMapping("app/getPraiseCount")
    @ResponseBody
    public int getPraiseCount(HttpServletRequest req){
		String targetId = req.getParameter("targetId");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("targetId", targetId);
		return praiseService.getPraiseCount(params);
    }
    
}
